﻿using Microsoft.EntityFrameworkCore;
using T_Person.BaseRep.Impls;
using T_Person.BaseRep.Interface;
using T_Person.BaseRep;

namespace T_Person.Read.APi.Expention
{
    public static class SerSerways
    {
        public static IServiceCollection AddService(this IServiceCollection services)
        {
            services.AddTransient(typeof(IBaseRepInterface<>), typeof(BaseWays<>));
            services.AddScoped<IEmployeeRep, EmployeeRepways>();
            services.AddScoped<IPersonnelRep, PersonnelRepways>();
            return services;
        }
        public static IServiceCollection AddDbcontextService<T>(this IServiceCollection services, string? con, bool enableSqlLog) where T : DbContext
        {
            return services.AddDbContext<T>(m =>
            {
                m.UseSqlServer(con, action =>
                {
                    action.EnableRetryOnFailure(
                        maxRetryCount: 10,
                        maxRetryDelay: TimeSpan.FromSeconds(60),
                        errorNumbersToAdd: null);
                }).EnableSensitiveDataLogging();
                if (enableSqlLog)
                {
                    m.UseLoggerFactory(LoggerFactory.Create(configure =>
                    {
                        configure.AddFilter((n, level) => n == DbLoggerCategory.Database.Command.Name && level == LogLevel.Information).AddConsole();
                    }));
                }
            });
        }
    }
}
